package com.ruoyi.business.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.business.domain.Notifications;
import com.ruoyi.business.service.INotificationsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 合同通知Controller
 *
 * @author ruoyi
 * @date 2025-02-13
 */
@RestController
@RequestMapping("/notifications")
public class NotificationsController extends BaseController
{
    @Autowired
    private INotificationsService notificationsService;

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-查询合同通知列表
     */
    @RequiresPermissions("business:notifications:list")
    @GetMapping("/list")
    public TableDataInfo list(Notifications notifications)
    {
        startPage();
        List<Notifications> list = notificationsService.selectNotificationsList(notifications);
        return getDataTable(list);
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-导出合同通知列表
     */
    @RequiresPermissions("business:notifications:export")
    @Log(title = "合同通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Notifications notifications)
    {
        List<Notifications> list = notificationsService.selectNotificationsList(notifications);
        ExcelUtil<Notifications> util = new ExcelUtil<Notifications>(Notifications.class);
        util.exportExcel(response, list, "合同通知数据");
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-获取合同通知详细信息
     */
    @RequiresPermissions("business:notifications:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(notificationsService.selectNotificationsById(id));
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-新增合同通知
     */
    @RequiresPermissions("business:notifications:add")
    @Log(title = "合同通知", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Notifications notifications)
    {
        return toAjax(notificationsService.insertNotifications(notifications));
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-修改合同通知
     */
    @RequiresPermissions("business:notifications:edit")
    @Log(title = "合同通知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Notifications notifications)
    {
        return toAjax(notificationsService.updateNotifications(notifications));
    }

    /**
     * 20250213 云计算-宏烨找房-05-王江波-我的合同任务工单V1.1-删除合同通知
     */
    @RequiresPermissions("business:notifications:remove")
    @Log(title = "合同通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(notificationsService.deleteNotificationsByIds(ids));
    }

    /**
     * 发送通知
     *
     * @param notification 通知信息
     * @return 操作结果
     */
    @PostMapping("/send")
    public AjaxResult sendNotification(@RequestBody Notifications notification) {
        try {
            notificationsService.sendNotification(notification);
            return AjaxResult.success("通知发送成功");
        } catch (Exception e) {
            return AjaxResult.error("通知发送失败：" + e.getMessage());
        }
    }

    /**
     * 获取用户未读通知列表
     *
     * @param userId 用户 ID
     * @return 未读通知列表
     */
    @GetMapping("/unread/{userId}")
    public AjaxResult getUnreadNotifications(@PathVariable Long userId) {
        try {
            List<Notifications> unreadNotifications = notificationsService.getUnreadNotifications(userId);
            return AjaxResult.success(unreadNotifications);
        } catch (Exception e) {
            return AjaxResult.error("获取未读通知列表失败：" + e.getMessage());
        }
    }
}

